layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let laydate = layui.laydate;
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: json.hasExportExcel ? ['filter', 'exports'] : ['filter'], done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
        page: true,
        limit: json.pageSize,
        height: 'full-185'
    });
    laypage.render(zmnPage.laypageOptions(json));
    laydate.render({
        elem: '#beginTimeStr',
        range: true,
        type: 'date',
    });
    laydate.render({
        elem: '#createTimeStr',
        range: true,
        type: 'date',
    });
    table.on('edit(zmn-table)', function (obj) {
        zmnLayUtil.ajaxSimpleUE("/push/noticeApp/setSort.action", {id: obj.data.id, sort: obj.value}, 0,
            function (result) {
                if (result.status == 200) {
                    layer.msg(result.message, {"icon": 1});
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            });
    });

    form.on('switch(status)', function (obj) {
        let status;
        let immediately = "";
        let editStatus = false;
        if (obj.elem.checked) {
            status = 2;
            let beginTime = $(this).attr("data_time");
            let date = new Date(beginTime);
            let nowDate = new Date();
            if (nowDate > date) {
                immediately = "检查到开始时间小于当前时间,请调整开始时间！";
                editStatus = true;
            } else {
                immediately = "该任务会在" + beginTime + "生效，是否确认？";
            }
        } else {
            status = 1;
            immediately = "确定要禁用当前数据？";
        }
        let dataPushType = $(this).attr("data-pushType");
        if (dataPushType === "推送消息" && status === 2) {
            if (editStatus){
                layer.confirm(immediately, {
                    btn: ['确定', '取消']
                }, function () {
                    openPage(obj.value,"修改");
                    window.location.reload();
                }, function(){
                    window.location.reload();
                });
            }else {
                layer.confirm(immediately, {
                    btn: ['确定', '取消']
                }, function () {
                    zmnLayUtil.ajaxSimpleUE("/push/noticeApp/setStatus.action", {id: obj.value, status}, 0,
                        function (result) {
                            if (result.status == 200) {
                                layer.msg(result.message, {"icon": 1});
                            } else {
                                layer.msg(result.message, {"icon": 2});
                            }
                        });
                    layer.msg("操作成功", {"icon": 1});
                }, function(){
                    window.location.reload();
                });
            }
        } else {
            zmnLayUtil.ajaxSimpleUE("/push/noticeApp/setStatus.action", {id: obj.value, status}, 0,
                function (result) {
                    if (result.status == 200) {
                        layer.msg(result.message, {"icon": 1});
                    } else {
                        layer.msg(result.message, {"icon": 2});
                    }
                });
        }
    });

    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event == 'add') {
            add();
            return false;
        } else if (obj.event == 'start') {
            let checkStatus = table.checkStatus(obj.config.id);
            let dataList = checkStatus.data;
            let ids = "";
            dataList.forEach(item => {
                ids += item.id + ",";
            });
            if (ids == "") {
                layer.msg('请选择需启用数据！', {icon: 2});
                return false;
            }
            layer.confirm('确定要启用已选择的数据？', {
                btn: ['确定', '取消']
            }, function () {
                let status = 2;
                zmnLayUtil.ajaxSimpleUE("/push/noticeApp/setStatusByList.action", {ids: ids, status: status}, 0,
                    function (result) {
                        if (result.status == 200) {
                            layer.msg(result.message, {"icon": 1});
                            window.location.reload();
                        } else {
                            layer.msg(result.message, {"icon": 2});
                        }
                    });
            });
        } else if (obj.event == 'stop') {
            let checkStatus = table.checkStatus(obj.config.id);
            let dataList = checkStatus.data;
            let ids = "";
            dataList.forEach(item => {
                ids += item.id + ",";
            });
            if (ids == "") {
                layer.msg('请选择需禁用数据！', {icon: 2});
                return false;
            }
            layer.confirm('确定要禁用已选择的数据？', {
                btn: ['确定', '取消']
            }, function () {
                let status = 1;
                zmnLayUtil.ajaxSimpleUE("/push/noticeApp/setStatusByList.action", {ids: ids, status: status}, 0,
                    function (result) {
                        if (result.status == 200) {
                            layer.msg(result.message, {"icon": 1});
                            window.location.reload();
                        } else {
                            layer.msg(result.message, {"icon": 2});
                        }
                    });
            });
        }
    });
    let add = function () {
        top.layui.index.openTabsPage("/push/noticeApp/addAndEditAppNotice.action?id=", '新增');
    };

    // 联动二级部门下啦框框
    form.on("select(deptOneId)", function (data) {
        zmnLayUtil.ajaxSimpleUE("/push/noticeApp/loadTwoDeptSelect.action", {pid: data.value}, 0, function (result) {
            let tempHtml = `<option value="">直接选择或搜索选择</option>`;
            $("#deptTwoId").html(tempHtml);
            if (result.status == 200) {
                result.data.forEach(items => {
                    let exAttr = "";
                    let exText = "";
                    tempHtml += '<option value="' + items.value + '" ' + exAttr + '>' + items.text + " " + exText + '</option>';
                });
                $("#deptTwoId").html(tempHtml);
                form.render("select");
            } else {
                layer.msg(result.message, {"icon": 2});
            }
        });
    });

    //对外暴露的接口
    exports('push_notice_list', {});
});

function openPage(id, text) {
    top.layui.index.openTabsPage("/push/noticeApp/addAndEditAppNotice.action?id=" + id, text);
}

